package vn.edu.hut.soict.translate.transform.utils;

import simplenlg.features.Feature;
import simplenlg.features.Form;
import simplenlg.features.NumberAgreement;
import simplenlg.features.Tense;
import simplenlg.framework.InflectedWordElement;
import simplenlg.framework.LexicalCategory;
import simplenlg.framework.NLGElement;
import simplenlg.framework.WordElement;

public class VerbTransformUtils extends WordTransformUtils {

	public static GrammaticalConstant form(String verb) {
		GrammaticalConstant form = beForm(verb);
		if (form != GrammaticalConstant.ERROR) {
			return form;
		}

		if (verb.equals(conjugate(verb, GrammaticalConstant.BARE))) {
			return GrammaticalConstant.BARE;
		}
		if (verb.equals(conjugate(verb, GrammaticalConstant.PRESENT_SIMPLE_WITH_SINGULAR))) {
			return GrammaticalConstant.PRESENT_SIMPLE_WITH_SINGULAR;
		}
		if (verb.equals(conjugate(verb, GrammaticalConstant.PRESENT_PARTICIPLE))) {
			return GrammaticalConstant.PRESENT_PARTICIPLE;
		}
		if (verb.equals(conjugate(verb, GrammaticalConstant.PAST_SIMPLE))) {
			return GrammaticalConstant.PAST_SIMPLE;
		}
		if (verb.equals(conjugate(verb, GrammaticalConstant.PAST_PARTICIPLE))) {
			return GrammaticalConstant.PAST_PARTICIPLE;
		}
		return GrammaticalConstant.ERROR;
	}

	public static GrammaticalConstant beForm(String verb) {
		if (verb.equalsIgnoreCase("be") || verb.equalsIgnoreCase("am") || verb.equalsIgnoreCase("are")) {
			return GrammaticalConstant.BARE;
		}
		if (verb.equalsIgnoreCase("is")) {
			return GrammaticalConstant.PRESENT_SIMPLE_WITH_SINGULAR;
		}
		if (verb.equalsIgnoreCase("being")) {
			return GrammaticalConstant.PRESENT_PARTICIPLE;
		}
		if (verb.equalsIgnoreCase("was") || verb.equalsIgnoreCase("were")) {
			return GrammaticalConstant.PAST_SIMPLE;
		}
		if (verb.equalsIgnoreCase("been")) {
			return GrammaticalConstant.PAST_PARTICIPLE;
		}
		return GrammaticalConstant.ERROR;
	}

	public static String conjugate(String verb, GrammaticalConstant form) {
		verb = verb.toLowerCase();
		if (verb.startsWith("to ")) {
			verb = verb.substring(3);
		}

		WordElement word = lexicon.getWordFromVariant(verb, LexicalCategory.VERB);
		InflectedWordElement inflWord = new InflectedWordElement(word);
		NLGElement element;

		switch (form) {
			case BARE:
				// e.g. fall
				return word.getBaseForm();
			case PRESENT_SIMPLE_WITH_SINGULAR:
				// e.g. falls
				inflWord.setFeature(Feature.TENSE, Tense.PRESENT);
				inflWord.setFeature(Feature.NUMBER, NumberAgreement.SINGULAR);
				element = realiser.realise(inflWord);
				return element.toString();
			case PRESENT_PARTICIPLE:
				// e.g. falling
				inflWord.setFeature(Feature.FORM, Form.PRESENT_PARTICIPLE);
				element = realiser.realise(inflWord);
				return element.toString();
			case PAST_SIMPLE:
				// e.g. fell
				inflWord.setFeature(Feature.TENSE, Tense.PAST);
				element = realiser.realise(inflWord);
				return element.toString();
			case PAST_PARTICIPLE:
				// e.g. fallen
				inflWord.setFeature(Feature.FORM, Form.PAST_PARTICIPLE);
				element = realiser.realise(inflWord);
				return element.toString();
		}

		return null;
	}

	public static String conjugateAs(String verb, String modelVerb) {
		GrammaticalConstant form = form(modelVerb);
		if (form != GrammaticalConstant.ERROR) {
			return conjugate(verb, form);
		}
		else {
			return null;
		}
	}

}
